﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Ext.Net;
using System.Collections.Generic;

public partial class Popup_People1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!X.IsAjaxRequest)
        {
            this.hdPrjCD.Text = Request.QueryString["Prj_CD"];
            this.hdDT.Text = Request.QueryString["DT"];
            this.hdNotIn.Text = Request.QueryString["NotIn"];

            ComboBind();

        }

        //2011.12.02 박승한 바로 조회
        if (!IsPostBack)
        {
            this.BindData(0);
        }
    }


    protected void CreateParameter(object sender, DirectEventArgs e)
    {
        string json = e.ExtraParams["Values"];

        Dictionary<string, string>[] dicValues = JSON.Deserialize<Dictionary<string, string>[]>(json);
        string strVal = string.Empty;

        foreach (Dictionary<string, string> dic in dicValues)
        {
            foreach (KeyValuePair<string, string> row in dic)
            {
                strVal += row.Value + "|";
            }
        }

        strVal = strVal.Remove(strVal.Length - 1);
        hdRowValue.Text = strVal;
    }

    private void ComboBind()
    {
        using (EmdisBest.WebService.Service oService = new EmdisBest.WebService.Service())
        {
            DataSet ds = oService.Select_DDDW_GROUPNM_ALL_S();

            this.StrCbx.DataSource = ds;
            this.StrCbx.DataBind();

            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                this.cbxTeam.SelectedIndex = 0;
            }
        }
    }

    protected void PopListSearch(object sender, DirectEventArgs e)
    {
        this.BindData(0);
    }


    protected void SetGrid()
    {
        using (EmdisBest.WebService.Service oService = new EmdisBest.WebService.Service())
        {
            DataSet ds = oService.Select_PA100T(this.hdPrjCD.Text, this.hdDT.Text, this.tbxName.Text, this.cbxTeam.Text, this.hdNotIn.Text);

            this.Store1.DataSource = ds;
            this.Store1.DataBind();
        }
    }

    protected void AddRecords(object sender, StoreRefreshDataEventArgs e)
    {
        this.BindData(int.Parse(e.Parameters["count"] ?? "0"));
    }

    private void BindData(int count)
    {
        //조회조건 넣었을 경우(완전하지 않음)
        //if (this.tbxName.Text.Length > 0 || (this.cbxTeam.Text != "%" &&  this.cbxTeam.Text.Length > 0))
        //{
        //    this.Store1.RemoveAll();
        //}

        this.Store1.RemoveAll();

        using (EmdisBest.WebService.Service oService = new EmdisBest.WebService.Service())
        {
            DataSet ds = oService.Select_PA100T(this.hdPrjCD.Text, this.hdDT.Text, this.tbxName.Text, this.cbxTeam.Text, this.hdNotIn.Text);
            DataTable dtSource = ds.Tables[0].Clone();

            

            int nCount = 0;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                nCount++;

                if (nCount <= count) continue;

                 dtSource.ImportRow(dr);
             
                if (nCount == (count + 50)) break;
            }

            dtSource.AcceptChanges();
            this.Store1.DataSource = dtSource;
            this.Store1.DataBind();

            InfoLabel.Text = String.Format("({0}/ {1})     ",count+ dtSource.Rows.Count, ds.Tables[0].Rows.Count);
            MoreButton.Disabled = (dtSource.Rows.Count) >= ds.Tables[0].Rows.Count;

        }
    }



    protected void btnInser_Click(object sender, DirectEventArgs e)
    {
        var win = new Window
        {
            ID = "Window2",
            //Title = "My Window",
            Height = 200,
            Width = 700,
            Padding = 5
        };


        Ext.Net.Button btn = (Ext.Net.Button)sender;
        string id = btn.ID;

        switch (id)
        {
            case "btnPeopleI":
                win.Listeners.Hide.Handler = "btnPeopleI.setDisabled(false);";
                win.AutoLoad.Url = "Popup_People_Add.aspx";
                win.Title = "인원 현황 추가항목";
                win.AutoLoad.Mode = LoadMode.IFrame;
                win.Render(this.Form);
                break;

            case "btnPeopleU":
                win.Listeners.Hide.Handler = "btnPeopleU.setDisabled(false);";
                win.AutoLoad.Url = "Popup_People_Add.aspx?code=" + this.hdRowValue.Text.Split('|')[0];
                win.Title = "인원 현황 추가항목";
                win.AutoLoad.Mode = LoadMode.IFrame;
                win.Render(this.Form);
                break;
            default:
                break;
        }
    }


    protected void btnDelete_Click(object sender, DirectEventArgs e)
    {
        try
        {
            using (EmdisBest.WebService.Service oService = new EmdisBest.WebService.Service())
            {
                int iResult = oService.DELETE_D_PA100T_MASTER_D(this.hdRowValue.Text.Split('|')[0]);
                BindData(0);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

}
